package com.api.workflow.web;

import com.api.workflow.util.YNHttpUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * MMI005 跨公司调拨订单提报【OA-》SAP】
 * */
public class YNMMI005Action extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        // 主表单名称
        String tableName = requestInfo.getRequestManager().getBillTableName();
        // 明细表单名称
        String detailTableName = tableName + "_dt1";
        try {
            JSONArray jsonArray1 = new JSONArray();
            //主表数据
            JSONObject result = new JSONObject();
            RecordSet re = new RecordSet();
            String czlxValue = "";
            String ycgpzbh = "";
            re.executeQuery("select * from " + tableName + " where requestid = " + requestid);
            if (re.next()) {
                czlxValue = re.getString("czlx");
                ycgpzbh = re.getString("ycgpzbh");
                result.put("ZHTBH", re.getString("hth"));
                result.put("UNSEZ", Util.null2String(new User(re.getInt("sqr")).getLastname()));
                result.put("BSART", re.getString("pzlx"));
                result.put("LIFNR", re.getString("gys"));
                result.put("EKORG", re.getString("cgzz"));
                result.put("EKGRP", re.getString("cgz"));
                result.put("WAERS", re.getString("hb"));
                result.put("WKURS", re.getString("hl"));
                result.put("BUKRS", re.getString("gsdm"));
                result.put("BEDAT", re.getString("cgddrq"));
                result.put("ZTERM", re.getString("fktj"));
                result.put("ZEBELN", re.getString("ycgpzbh"));//原采购凭证编号
            }

            int mainid = re.getInt("id");
            String lcbh = re.getString("lcbh");
            String hth = re.getString("hth");
            String czlx = re.getString("czlx");
            String ddlx = re.getString("ddlx");
            RecordSet rs = new RecordSet();
            rs.executeQuery("select * from " + detailTableName + " where mainid = " + mainid);
            JSONArray jsonArray = new JSONArray();
            int count = 10;
            while (rs.next()) {
                JSONObject jsonObject = new JSONObject();
                if (!czlx.equals("I")) {
                    jsonObject.put("EBELP", rs.getString("xh"));
                } else {
                    jsonObject.put("EBELP", count);
                }
                jsonObject.put("ZHTBH", hth);
                jsonObject.put("ZXGLX", rs.getString("czlx"));
                jsonObject.put("BANFN", rs.getString("cgsqbh"));
                jsonObject.put("BNFPO", rs.getString("cgsqdxmbh"));
                jsonObject.put("KNTTP", rs.getString("kmfplb"));
                jsonObject.put("PSTYP", rs.getString("xmlb"));
                jsonObject.put("ZMATNR", rs.getString("wl"));
                jsonObject.put("TXZ01", rs.getString("wlms"));
                jsonObject.put("ANMNG", rs.getString("cgsl"));
                jsonObject.put("MEINS", rs.getString("dddw"));
                jsonObject.put("WERKS", rs.getString("gc"));
                jsonObject.put("EINDT", rs.getString("jhrq"));
                jsonObject.put("KOSTL", rs.getString("cbzx"));
                jsonObject.put("AUFNR", rs.getString("nbdd"));
                jsonObject.put("MATKL", rs.getString("wlz"));
                jsonObject.put("LGORT", rs.getString("kcdd"));
                //订单类型
//                if ("ZSTO".equals(ddlx)) {
//                    jsonObject.put("NETPR", rs.getString("jj"));
//                } else {
//                    jsonObject.put("NETPR", rs.getString("hsdj"));
//                }
                jsonObject.put("NETPR", rs.getString("hsdj"));
                jsonObject.put("PEINH", rs.getString("jgdw"));
                jsonObject.put("MWSKZ", rs.getString("sm"));
                jsonObject.put("ZBZGG", rs.getString("bzgg"));
                jsonObject.put("VBELN", rs.getString("xsddh"));
                jsonObject.put("VBELP", rs.getString("xsddx"));
                jsonObject.put("ZJSFS", rs.getString("jsfs"));
                jsonObject.put("ZKSCHA1", rs.getString("tjlx1"));
                jsonObject.put("ZKBETR1", rs.getString("je1"));
                jsonObject.put("ZLIFNR1", rs.getString("gysbh"));
                jsonObject.put("ZKSCHA2", rs.getString("tjlx2"));
                jsonObject.put("ZKBETR2", rs.getString("je2"));
                jsonObject.put("ZLIFNR2", rs.getString("gysbm"));
                jsonObject.put("ZLIFNR", rs.getString("sccj"));
                jsonObject.put("ZXSDD", rs.getString("xspz"));
                jsonObject.put("ZXSDDS", rs.getString("xspzxm"));
                jsonObject.put("UEBTO", rs.getString("gljhrc"));
                jsonObject.put("UNTTO", rs.getString("jhbzrc"));
                jsonObject.put("BPRME", rs.getString("ddjgdw"));
                jsonObject.put("BPUMZ", rs.getString("ddjgdwsl"));
                String id = String.valueOf(rs.getString("id"));
                RecordSet recordSet = new RecordSet();
                baseBean.writeLog("1111111111" + id);
                if (!czlx.equals("I")) {
                    recordSet.executeUpdate("update " + detailTableName + " set xh = '" + rs.getString("xh") + "' where id = " + id);
                    baseBean.writeLog("数据执行成功");
                } else {
                    recordSet.executeUpdate("update " + detailTableName + " set xh = '" + count + "' where id = " + id);
                    baseBean.writeLog("数据执行成功");
                }

                jsonArray.add(jsonObject);
                count = count + 10;
            }
            result.put("ITEMS", jsonArray);
            jsonArray1.add(result);
            baseBean.writeLog("wyq===MMI005采购订单创建及变更接口===jsonArray1" + jsonArray1.toString());
            String post = YNHttpUtils.MMI005(jsonArray1.toString(), YNHttpUtils.token());
            baseBean.writeLog("wyq===MMI005采购订单创建及变更接口===post" + post);
            JSONObject jsonObject = JSONObject.parseObject(post);
            JSONArray items = jsonObject.getJSONArray("ITEMS");
            RecordSet recordSet = new RecordSet();
            //qqcs
            String updateMainTableName = "update " + tableName + " set   qqcsx = ? , fhjgx = ?  where  requestid = ? ";
            recordSet.executeUpdate(updateMainTableName, jsonArray1.toString(), post, requestid);

            for (int i = 0; i < items.size(); i++) {
                JSONObject jsonObject1 = items.getJSONObject(i);
                String STATUS = jsonObject1.getString("STATUS");
                String MESSAGE = jsonObject1.getString("MESSAGE");
                String ZHTBH = jsonObject1.getString("ZHTBH");


                if (!"S".equals(STATUS)) {
                    requestInfo.getRequestManager()
                            .setMessageid(requestInfo.getRequestid() + "MMI005");
                    requestInfo.getRequestManager().setMessagecontent("SAP接口异常，请联系SAP人员，错误信息提示::" + MESSAGE);
                    return Action.FAILURE_AND_CONTINUE;

                }

                //回传凭证号
                String EBELN = jsonObject1.getString("ZEBELN");
                String update = "update " + tableName + " set  cgpzh = ?   where  requestid = ? ";
                recordSet.executeUpdate(update, EBELN, requestid);
                writeLog("修改完成");

            }


        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager()
                    .setMessageid(requestInfo.getRequestid() + "MMI005");
            requestInfo.getRequestManager().setMessagecontent("接口异常:" + e.toString());
            return Action.FAILURE_AND_CONTINUE;

        }


        return Action.SUCCESS;

    }


    /**
     * 提取明细表全部的数据
     *
     * @param requestInfo
     * @return
     */
    private static List<Map<String, Object>> getMaps(RequestInfo requestInfo) {
        DetailTable[] detailTable = requestInfo.getDetailTableInfo().getDetailTable();
        //这里是存储了全部的明细表数据  可自行修改存储结构
        List<Map<String, Object>> detailTableMap = new ArrayList<>();
        if (detailTable.length > 0) {
            for (int i = 0; i < detailTable.length; i++) {
                DetailTable dt = detailTable[i];
                Row[] s = dt.getRow();
                for (int j = 0; j < s.length; j++) {
                    Row r = s[j];
                    Cell c[] = r.getCell();
                    Map<String, Object> objectObjectHashMap = new HashMap<>();
                    for (int k = 0; k < c.length; k++) {
                        Cell c1 = c[k];
                        String name = c1.getName().toLowerCase();
                        String value = c1.getValue();
                        objectObjectHashMap.put(name, value);
                    }
                    detailTableMap.add(objectObjectHashMap);
                }
            }
        }
        return detailTableMap;
    }


}